package main

import "fmt"

func main() {
	nums := []int{3, 2, 2, 3}
	val := 3
	element := removeElement2(nums, val)
	println(element)
}

func removeElement(nums []int, val int) int {
	left := 0
	for _, v := range nums { // v 即 nums[right]
		if v != val {
			nums[left] = v
			left++
		}
	}
	return left
}

func removeElement2(nums []int, val int) int {
	left := 0
	right := len(nums)
	for left < right {
		if nums[left] == val {
			nums[left] = nums[right-1]
			right--
		} else {
			left++
		}
	}
	fmt.Printf("%v\n", nums)
	return left
}
